/**
 * created by dqn 2018/3/7
 */

package main.service.impl;

import main.entity.WebUser;
import main.repository.WebUserRepository;
import main.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthorizationServiceImpl implements main.service.AuthorizationService {
    @Autowired
    WebUserRepository userRepository;
    @Autowired
    MD5Util md5Util;

    /**
     * 登陆
     *
     * @param webUser
     * @return
     */
    @Override
    public WebUser login(WebUser webUser) {
        String mded = md5Util.getEncodedMD5(webUser.getNumber() + webUser.getPassword());
        return userRepository.getWebUserByNumberAndPassword(webUser.getNumber(), mded).orElse(null);
    }

    /**
     * 注册
     *
     * @param webUser
     * @return
     */
    @Override
    public WebUser signUp(WebUser webUser) {
        String passwd = md5Util.getEncodedMD5(webUser.getNumber() + webUser.getPassword());
        webUser.setPassword(passwd);
        webUser.setPermission(WebUser.COMMONUSER);// 普通用户
        userRepository.save(webUser);
        WebUser newUser = userRepository.getWebUserByNumberAndPassword(webUser.getNumber(), passwd).orElse(null);
        return newUser;
    }
}
